import axios from 'axios'
import Cookie from 'js-cookie'
import router from "../router";
// create an axios instance
const service = axios.create({
  baseURL: 'http://localhost:80', // url = base url + request url
  timeout: 5000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // 这里判断你是否有携带token
    if (Cookie.get('token')) {
      // 将token放到请求包的头部中
      config.headers['authorization'] = getToken()
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    const res = response.data
    return res
  },
  error => {
    console.log('err' + error) // for debug
    return Promise.reject(error)
  }
)

export default service
